package com.tianqu.tms.www.car.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.MarketCarGateInfo;

import ice_ipcsdk.SDK;

/** 
 * 手动抬杆操作
 * @author mufeiyu
 */
public class CarSwitchOffReleaseAjaxAction extends TmsAjaxAction{
	private Logger logger = Logger.getLogger(CarSwitchOffReleaseAjaxAction.class);
	private static final String GATEID = "gateId";
	private static final String LICENSE_PLATE = "licensePlate";
	private static final String TEMPLICENS_PLATE = "tempLicensePlate";

	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		controller.begin();
		try {
			String gateId = request.getParameter(GATEID);
			String licensePlate = request.getParameter(LICENSE_PLATE);
			String tempLicensePlate = request.getParameter(TEMPLICENS_PLATE);
			String deviceIp = "";
			Codes code = Codes.getInstance();
			if(!"0".equals(gateId)&&!StringUtil.isNullOrBlank(gateId)){
				deviceIp = code.getItemValueByItemIdAndItemCode(Codes.GATEWAY_IP, Integer.valueOf(gateId));
			}
			// 一体杆SDK集合
			SDK sdk = new SDK();
			
			sdk.ICE_IPCSDK_Open(deviceIp, null);
			
			try {
	            Thread.sleep(1000);
	        } catch (InterruptedException e) {
	            e.printStackTrace(); 
	        }
			
			//抬杆
			sdk.ICE_IPCSDK_OpenGate();
			
			sdk.ICE_IPCSDK_Close();
			
			
			//拼select
			StringBuffer str = new StringBuffer();
			
			String responseText =str.toString();
			controller.commit();
			
			//
			String  gateType= code.getItemValueByItemIdAndItemCode(Codes.GATEWAY_TYPE,Integer.valueOf(gateId));
			
			Car car = Car.getInstance();
			if(StringUtil.isNotBlank(licensePlate)) {
				car = Car.getInstance().findCarByLicensePlate(licensePlate,1);
			}else if(StringUtil.isNotBlank(tempLicensePlate)) {
				car = Car.getInstance().findCarByLicensePlate(tempLicensePlate,2);
			}
			
			TmsUserProfile profile = uc.getUserProfile();
			MarketCarGateInfo marketCarGateInfo = MarketCarGateInfo.getInstance();
			if(StringUtil.isNotBlank(car.getSerial())) marketCarGateInfo.setSerial(car.getSerial());
			else marketCarGateInfo.setSerial("");
			if(!"0".equals(gateId)&&!StringUtil.isNullOrBlank(gateId))  marketCarGateInfo.setGateId( Integer.valueOf(gateId));
			marketCarGateInfo.setLicensePlate(StringUtil.isNullOrBlank(licensePlate)?"":licensePlate);
			marketCarGateInfo.setTempLicensePlate(StringUtil.isNullOrBlank(tempLicensePlate)?"":tempLicensePlate);
			marketCarGateInfo.setGateType(StringUtil.isNullOrBlank(gateType)?"":gateType);
			marketCarGateInfo.setLocatedIP("");
			marketCarGateInfo.setCarPhoto("");
			marketCarGateInfo.setCarColor("0");
			marketCarGateInfo.setLicenseColor(0);
			marketCarGateInfo.setThreadType(3);//人工处理
			marketCarGateInfo.setThreadStatus(99);//人工处理成功
			marketCarGateInfo.setCheckFlag(2);//on
			marketCarGateInfo.setUpdateUserId(profile.getUniqueId());
			marketCarGateInfo.insertGateInfo(marketCarGateInfo);
			
			responseValues(response, responseText, TEXT);
		}catch (TmsException e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		
		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}
}
